<?php

$fun[c['up']] = function ($cmd) {
  if (
    !isset($cmd['token']) ||
    !isset($cmd['table']) ||
    strIn($cmd['table'], [',', ' ', '.'])
  )
    return ['msg' => '必要属性 token,table缺失或table含有非法字符', 'data' => $cmd];
  if (!is_array($cmd['where'])) $cmd['where'] = [];
  $cmd['where']['table'] = $cmd['table'];
  $no = ['msg' => '当前条件下无订单', 'data' => $cmd['where']];
  $ret = useToken($cmd);
  $time = time();
  if ($ret) {
    if (!strcasecmp($cmd['table'], 'users')) { // 用户名
      $tmp = [
        'table' => 'users',
        'changeTime' => $time,
        'where' => $cmd['where']
      ];
      $ask = 0;
      if ($cmd['password']) $tmp['password'] = $cmd['password'];
      if ($cmd['name']) $tmp['name'] = $cmd['name'];
      if ($cmd['uID']) $tmp['uID'] = (int) $cmd['uID'];
      if ($cmd['uID'] || $cmd['name']) $ask += 4;
      if ($cmd['tel']) {
        $tmp['tel'] = $cmd['tel'];
        $ask += 2;
      }
      if ($cmd['display']) $tmp['display'] = json_encode($cmd['display'], JSON_UNESCAPED_UNICODE);
      if ($cmd['recv']) $cmd['recv'] = json_encode($cmd['recv'], JSON_UNESCAPED_UNICODE);
      if ($cmd['state']) $tmp['state'] = $ret['type'] == 管理员 ? (int) $cmd['state'] : 1;
      if ($cmd['type']) $tmp['type'] = $ret['type'] == 管理员 ? (int) $cmd['type'] : 会员;
      if ($cmd['itemTypeCid']) $tmp['itemTypeCid'] =  $ret['type'] == 管理员 ? $cmd['itemTypeCid'] : 0;
      if ($cmd['other']) $tmp['other'] = $cmd['other'];
      if ($cmd['ask']) $tmp['ask'] = $cmd['ask'];
      if ($ret['type'] != 管理员) $tmp['where']['ID'] = $ret['ID'];
      $cmd = $tmp;
    } elseif (!strcasecmp($cmd['table'], 'items') && $ret['type'] == 商家) { // 商品
      if (
        $ret['type'] == 商家 &&
        8 == $ret['state'] & 8 && // 商户认证通过
        $ret['itemTypeCid'] & (int) $cmd['type'] == (int) $cmd['type'] ||
        $ret['type'] == 管理员
      ) {
        $tmp = [
          'table' => 'items',
          'changeTime' => $time,
          'cuid' => $ret['ID'],
        ];
        if ($cmd['sellID']) $tmp['sellID'] = $ret['ID'] == 管理员 ? (int) $cmd['sellID'] : $ret['ID'];
        if ($cmd['name']) $tmp['name'] = $cmd['name'];
        if ($cmd['CNY']) $tmp['CNY'] = (int) $cmd['CNY'];
        if ($cmd['point']) $tmp['point'] = (int) $cmd['point'];
        if ($cmd['sum']) $tmp['sum'] = (int) $cmd['sum'];
        if (isset($cmd['state'])) $tmp['state'] = (int) $cmd['state'];
        if ($cmd['power']) $tmp['power'] = $ret['ID'] == 管理员 ? (int) $cmd['power'] : 0;
        if ($cmd['imgs']) $tmp['imgs'] = json_encode($cmd['imgs'], JSON_UNESCAPED_UNICODE);
        if ($cmd['info']) $tmp['info'] = $cmd['info'];
        if ($cmd['other']) $tmp['other'] = $cmd['other'];
        // 'typeCid' => (int) $cmd['type'],
        if ($cmd['typeCid'] && $ret['itemTypeCid'] & $cmd['typeCid'] == $cmd['typeCid']) $tmp['typeCid'] = (int) $cmd['typeCid'];
      } else return noPrem;
    } elseif (!strcasecmp($cmd['table'], 'orders')) { // 交易记录
      reset:
      // 开始判断
      switch ($ret['type']) {
        case 会员:
          $cmd['where']['buyID'] = $ret['ID'];
          $cmd2 = [
            'table' => 'orders',
            'changeTime' => $time,
            'where' => $cmd['where']
          ];
          $tmp = GetData($cmd['where'], '*', true);
          if ($tmp) {
            $item = GetData(['table' => 'items', 'ID' => $tmp['ID']], '*', true);
            if (!$item) $item = json_decode($tmp['itemInfo'], true);
            if (!$cmd['sum']) $cmd['sum'] = $tmp['sum'];
            if ($tmp['state'] == 1 || $cmd['state'] == 0) { // 未支付
              $cmd2['money'] = [];
              if ($item['CNY']) $cmd2['money'][] = $item['CNY'] * $cmd['sum'];
              if ($item['point']) $cmd2['money'][] = $item['point'] * $cmd['sum'];
              $cmd2['money'] = json_encode($cmd2['money']);

              if ($cmd['state'] == 0 || $cmd['state'] == 1) $cmd2['state'] = $cmd['state']; //可设置关闭或开启
            } elseif ($tmp['state'] == 2 || $tmp['state'] == 3) { // 已支付。待配送 待收货
              if ($cmd['state'] == 4 || $cmd['state'] == 6) $cmd2['state'] =  $cmd['state']; // 可设置确认收货(待评价)/提出退款
            } elseif ($tmp['state'] == 4) { //等待评价
              if ($cmd['state'] == 5 || is_array($cmd['itemEval'])) {
                $cmd['state'] = 5;
                AddData([
                  'createTime' => $time,
                  'changeTime' => $time,
                  'cuid' => $ret['ID'],
                  'user' => $ret['ID'],
                  'name' => $cmd['itemEval']['name'],
                  'info' => $cmd['itemEval']['info'],
                  'score' => (int) $cmd['itemEval']['score'],
                  'itemID' => $item['ID']
                ]);
              }
            } elseif ($tmp['state'] > 5 && $tmp['state'] < 10 && $cmd['state'] == 4) { // 可随时更改状态为确认收货
              $cmd['state'] = 4;
            } else {
              return ['msg' => '当前订单状态不允许修改', 'data' => $tmp];
            }
            $cmd = $cmd2;
            break;
          }
          return $no;
        case 技师:
          $cmd['where']['sellID'] = $ret['ID'];
          $tmp = GetData($cmd['where'], '*', true);
          if ($tmp) {
            if ($tmp['state'] == 2 && $cmd['state'] == 3) $cmd2['state'] = 3;
            elseif ($tmp['state'] == 6) {
              if ($cmd['state'] == 7 || $cmd['state'] == 8) $cmd2['state'] = $cmd['state'];
            } else return ['msg' => '当前订单状态不允许修改', 'data' => $tmp];
          } else return $no;
          $cmd = ['table' => 'transactionLog', 'state' => 3, 'where' => $cmd['where']];
          break;
        case 商家:
          $tmp = GetData($cmd['where'], 'sellID,buyID', true);
          if ($tmp) { // 走技师流程
            if ($tmp['sellID'] == $ret['ID']) $ret['type'] = 技师;
            elseif ($tmp['buyID'] == $ret['ID']) $ret['type'] = 会员;
            else return noPrem;
            goto reset;
          }
          return noPrem;
        case 管理员:
          # code...
          break;

        default:
          return ['msg' => 'token非法', 'data' => $cmd];
          break;
      }
    } elseif (strcasecmp($cmd['table'], 'itemEval') == 0) { // 评价
      $cmd['where']['user'] = $ret['ID'];
      $tmp = GetData($cmd['where'], 'ID,createTime');
      if (!$tmp) return $no;
      foreach ($tmp as $value) {
        $t = (int) $value['createTime'] ? $value['createTime'] : strtotime($value['createTime']);
        $t += 7 * 24 * 60 * 60;
        if ($t < time()) return ['msg' => '只允许修改7天内的评价', 'data' => $value];
      }
      $cmd2 = ['table' => 'itemEval', 'cuid' => $ret['ID'], 'where' => $cmd['where']];
      if ($cmd['name']) $cmd2['name'] = $cmd['name'];
      if ($cmd['info']) $cmd2['info'] = $cmd['info'];
      if ($cmd['score']) $cmd2['score'] = (int) $cmd['score'];
      $cmd = $cmd2;
    } else {
      return noPrem;
    }
    $ret = ['result' => UpData($cmd)];
  } else $ret = ['msg' => 'token失效', 'data' => $cmd];
  return $ret;
};

$fun[c['add']] = function ($cmd) {
  $ret = useToken($cmd);
  if (!strcasecmp($cmd['table'], 'users')) { // 用户
    $t = GetData(['table' => 'users', 'username' => $cmd['username']], 'ID');
    if ($t) return ['msg' => '用户名已存在', 'data' => $cmd];
    $t = time();
    $cmd = [
      'table' => 'users',
      'createTime' => $t,
      'changeTime' => $t,
      'useTime' => $t,
      'username' => $cmd['username'],
      'password' => $cmd['password'],
      'name' => $cmd['name'],
      'uID' => $cmd['uID'],
      'tel' => $cmd['tel'],
      'display' => json_encode($cmd['display']),
      'recv' => json_encode($cmd['recv']),
      'state' => $ret['type'] == 管理员 && $cmd['state'] ? (int) $cmd['state'] : 1,
      'type' => $ret['type'] == 管理员 && $cmd['type'] ? (int) $cmd['type'] : 会员,
      'itemTypeCid' => $ret['type'] == 管理员 && $cmd['itemTypeCid'] ? (int) $cmd['itemTypeCid'] : 0,
      'other' => $cmd['other'],
      'ask' => (int) $cmd['ask']
    ];
    $ret = ['msg' => '', 'data' => AddData($cmd)];
  } elseif ($ret) {
    $t = time();
    if (!strcasecmp($cmd['table'], 'items')) { // 商品项目
      if (
        $ret['type'] == 商家 &&
        8 == $ret['state'] & 8 && // 商户认证通过
        $ret['itemTypeCid'] & (int) $cmd['type'] == (int) $cmd['type'] ||
        $ret['type'] == 管理员
      ) {
        $y = $cmd;
        $cmd = [
          'table' => 'items',
          'createTime' => $t,
          'changeTime' => $t,
          'useTime' => 0,
          'cuid' => $ret['ID'],
          'sellID' => $ret['ID'] == 管理员 && $cmd['sellID'] ? $cmd['sellID'] : $ret['ID'],
          'name' => $cmd['name'],
          'CNY' => (float) $cmd['CNY'],
          'point' => (float) $cmd['point'],
          'sum' => (float) $cmd['sum'],
          'state' => isset($cmd['state']) ? (int) $cmd['state'] : 1,
          'power' => $ret['ID'] == 管理员 && $cmd['power'] ? (int) $cmd['power'] : 0,
          'imgs' => json_encode($cmd['imgs'], JSON_UNESCAPED_UNICODE),
          'info' => $cmd['info'],
          'other' => $cmd['other'],
          'typeCid' => (int) $cmd['type'],
        ];
      } else return noPrem;
    } elseif (!strcasecmp($cmd['table'], 'orders')) { // 订单系统
      $tmp = [];
      foreach ($cmd['items'] as $value) {
        $item = GetData(['table' => 'items', 'ID' => $value['ID']], '*', true);
        if ($item) {
          $tmp[] = [
            'table' => 'orders',
            'createTime' => $t,
            'changeTime' => $t,
            'cuid' => $ret['ID'],
            'state' => $ret['ID'] == 管理员 && $value['state'] ? $value['state'] : 1,
            'typeCid' => $item['typeCid'],
            'money' => items2money(['CNY' => $item['CNY'] * $value['sum'], 'point' => $item['point'] * $value['sum']]),
            'payment' => 0, // 将删除
            'sellID' => $item['sellID'],
            'buyID' => $ret['ID'] == 管理员 && $value['buyID'] ? $value['buyID'] : $ret['ID'],
            'itemID' => $item['ID'],
            'sum' => $value['sum'],
            'name' => $value['name'],
            'tel' => $value['tel'],
            'address' => $value['address'],
            'itemInfo' => $item,
            'other' => $item['other'],
          ];
        }
      }
      $cmd = $tmp;
    } elseif (!strcasecmp($cmd['table'], 'itemType')) { // 商品分类
      $cmd = [
        'table' => 'itemType',
        'createTime' => $t,
        'changeTime' => $t,
        'cuid' => $ret['ID'],
        'name' => $cmd['name'],
        'cid' => 0
      ];
    } else {
      return noPrem;
    }
    $ret = ['result' => AddData($cmd)];
    if (!strcasecmp($cmd['table'], 'itemType')) { // 设置cid
      runSql('UPDATE itemtype SET cid=1<<(ID-1) WHERE cid=0');
    }
  } else $ret = ['msg' => 'token失效', 'data' => $cmd];
  return $ret;
};


?>